package com.market.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.market.sys.entity.SubComment;
import com.market.sys.entity.User;
import com.market.sys.mapper.SubCommentMapper;
import com.market.sys.mapper.UserMapper;
import com.market.sys.service.ISubCommentService;
import com.market.sys.util.EmptyUtil;
import com.market.sys.util.TimeUtil;
import com.market.sys.vo.SubCommentVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.market.sys.util.Constants.DOWNLOAD_URL;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xd
 * @since 2024-03-26
 */
@Service
public class SubCommentServiceImpl extends ServiceImpl<SubCommentMapper, SubComment> implements ISubCommentService {


    @Resource
    private UserMapper userMapper;

    @Override
    public List<SubCommentVO> fetchSubCommentList(Integer commentId, Integer state) {
        LambdaQueryWrapper<SubComment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SubComment::getParentCommentId, commentId);
        if (!EmptyUtil.isNumberEmpty(state)){
            lambdaQueryWrapper.eq(SubComment::getState, state);
        }
        List<SubComment> subCommentList = this.baseMapper.selectList(lambdaQueryWrapper);
        return getSubCommentVOList(subCommentList);
    }

    @Override
    public List<SubCommentVO> fetchSubCommentList(Map<String, Object> paramMap) {
        int pageNum = (int)(paramMap.get("pageNum"));
        int pageSize = (int)(paramMap.get("pageSize"));
        Page<SubComment> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SubComment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SubComment::getParentCommentId, String.valueOf(paramMap.get("parentCommentId")));
        if (!EmptyUtil.isStringEmpty(String.valueOf(paramMap.get("state")))){
            lambdaQueryWrapper.eq(SubComment::getState, String.valueOf(paramMap.get("state")));
        }
        Page<SubComment> subCommentPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        return getSubCommentVOList(subCommentPage.getRecords());
    }

    private List<SubCommentVO> getSubCommentVOList(List<SubComment> subCommentList){
        List<SubCommentVO> commentVOList = new ArrayList<>();
        for (SubComment subComment : subCommentList) {
            commentVOList.add(convertCommentToVO(subComment));
        }
        return commentVOList;
    }

    private SubCommentVO convertCommentToVO(SubComment comment){
        SubCommentVO commentVO = new SubCommentVO();
        comment.setTime(TimeUtil.completeGetTimeStamp(comment.getTime()));

        // 查询用户
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getId, comment.getUserId());
        User user = userMapper.selectOne(userLambdaQueryWrapper);

        BeanUtil.copyProperties(comment, commentVO, false);
        commentVO.setNickName(user.getNickName());
        if (!EmptyUtil.isStringEmpty(user.getAvatar())){
            commentVO.setAvatar(DOWNLOAD_URL + user.getAvatar());
        }

        LambdaQueryWrapper<SubComment> subCommentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        subCommentLambdaQueryWrapper.eq(SubComment::getParentCommentId, comment.getId());
        if (!EmptyUtil.isNumberEmpty(comment.getState())){
            subCommentLambdaQueryWrapper.eq(SubComment::getState, comment.getState());
        }
        List<SubComment> subComments = this.baseMapper.selectList(subCommentLambdaQueryWrapper);
        if (subComments.size() >= 3){
            subComments.subList(0, 2);
        }
        return commentVO;
    }
}
